﻿/*********************************************************************
 * @file       clock_block.cpp
 * @brief      闹钟小组件类
 * @details    基于ui文件运行
 * @author     南樟(a2169272059@163.com)
 * @date       2025-2-10
 * @version    1.0.0
 * @copyright  a2169272059@163.com
 *********************************************************************/
#include "ui_clock_block.h"
#include <QMessageBox>
#include <QMessageBox>
#include <QTime>

#include "clock_block.h"
#include "sqlite.h"
#include "create_clock.h"
#include "ticktimer.h"
#include "mainwindow.h"
/// 数据库对象类
extern sqlite db;
/// 主滴答触发器
extern tickTimer *g_ticker;
/// 主窗口堆叠stackedWidget
extern QStackedWidget *g_mainWidget;
/**
 * @brief clock_block::clock_block 闹钟小组件构造函数
 * @param parent 父节点
 * @param id 在数据库中对应的id
 * @param note 闹钟的备注
 * @param time 闹钟的闹铃时间
 * @param enable 闹铃开关
 * @param week_day 以bits位方式记录的星期信息
 * @note 当week_day的二进制对应0100100时，
 * 意味着这个闹钟在周三和周六启用。在其他星期是关闭的状态
 */
clock_block::clock_block(QWidget *parent
                         ,const int &id,const QString &note
                         ,const QString &time,const int& enable
                         ,const int &week_day)
    : QWidget(parent)
    , ui(new Ui::clock_block)
    ,m_id(id)
{
    ui->setupUi(this);
    ui->checkBox->setChecked(bool(enable));
    ui->lab_time->setText(time);
    ui->note_text->setText(note);
    if(week_day & (0x01<<7)){
        ui->lab_week7->setStyleSheet(
            "color:blue;");
    }else{
        ui->lab_week7->setStyleSheet(
            "color:gray;");
    }
    if(week_day & (0x01<<1)){
        ui->lab_week1->setStyleSheet(
            "color:blue;");
    }else{
        ui->lab_week1->setStyleSheet(
            "color:gray;");
    }
    if(week_day & (0x01<<2)){
        ui->lab_week2->setStyleSheet(
            "color:blue;");
    }else{
        ui->lab_week2->setStyleSheet(
            "color:gray;");
    }
    if(week_day & (0x01<<3)){
        ui->lab_week3->setStyleSheet(
            "color:blue;");
    }else{
        ui->lab_week3->setStyleSheet(
            "color:gray;");
    }
    if(week_day & (0x01<<4)){
        ui->lab_week4->setStyleSheet(
            "color:blue;");
    }else{
        ui->lab_week4->setStyleSheet(
            "color:gray;");
    }
    if(week_day & (0x01<<5)){
        ui->lab_week5->setStyleSheet(
            "color:blue;");
    }else{
        ui->lab_week5->setStyleSheet(
            "color:gray;");
    }
    if(week_day & (0x01<<6)){
        ui->lab_week6->setStyleSheet(
            "color:blue;");
    }else{
        ui->lab_week6->setStyleSheet(
            "color:gray;");
    }
}
/**
 * @brief clock_block::~clock_block 闹钟小组件的析构函数
 */
clock_block::~clock_block()
{
    delete ui;
}
/**
 * @brief clock_block::on_checkBox_clicked 小组件上的闹钟开关的更新逻辑
 * @param checked
 */
void clock_block::on_checkBox_clicked(bool checked)
{
    QSqlQuery query(db.get_db());
    if(checked){
        query.prepare("UPDATE clock "
                   "SET enable = 1 "
                    "WHERE id = :id");
    }else{
        query.prepare("UPDATE clock "
                      "SET enable = 0 "
                      "WHERE id = :id");
    }
    query.bindValue(":id",m_id);
    if(!query.exec()){
        qDebug()<<"更新语句执行失败";
        QMessageBox::warning(this,"警告","数据更新失败,\n这可能意味着闹钟界面数据已错乱");
        return;
    }
    g_ticker->load_clock();
    g_ticker->start();
}

/**
 * @brief clock_block::on_pushButton_clicked 闹钟小组件上编辑按钮的触发逻辑
 */
void clock_block::on_pushButton_clicked()
{
    QSqlQuery query(db.get_db());
    query.prepare("SELECT id, note, clock_time, bits_week, enable "
                  "FROM clock "
                  "WHERE id = :id");
    query.bindValue(":id",m_id);
    if(!query.exec()){
        qDebug()<<"数据编辑失败"<<query.lastError();
        QMessageBox::warning(this,"错误","找不到对应的数据");
        return;
    }
    query.next();
    create_clock *tmpWidget = new create_clock(this);
    tmpWidget->change2Edit(query.value(0).toInt()
                           ,QTime::fromString(query.value(2).toString(),"HH:mm")
                           ,query.value(1).toString()
                           ,query.value(3).toInt());
    connect(tmpWidget,&create_clock::back2Clock,this,[this,tmpWidget](){
        tmpWidget->deleteLater();
        g_ticker->load_clock();
        g_ticker->start();
        g_mainWidget->setCurrentIndex(MainWindow::index_clock);
    });
    // 更改组件信息
    // tmpWidget->setNoteEdit(query.value(1).toString());
    // tmpWidget->setTimeEdit(QTime::fromString(query.value(3).toString(),"HH:mm"));
    g_mainWidget->addWidget(tmpWidget);
    g_mainWidget->setCurrentWidget(tmpWidget);
}


